using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Global;

public partial class controls_usrIdType : System.Web.UI.UserControl
{
    protected string strIdType = "Select";
    protected string strValidatinoGroup = "";
    protected string strClientScript = "";
    protected string strErrorMessage = "Choose ID type";
    protected bool bEnabled = true;
    protected bool bReadOnly = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //DataTable dtIdType = General.GetComboItem("TH.IdType", false);
            //ddlIdType.DataSource = dtIdType;
            //ddlIdType.DataTextField = dtIdType.Columns[0].ColumnName.ToString();
            //ddlIdType.DataValueField = dtIdType.Columns[1].ColumnName.ToString();
            //ddlIdType.DataBind();
            //ddlIdType.Items.Insert(0, new ListItem("Select", "-1"));

            //ddlIdType.Attributes.Add("onblur", "ShowValidationEffect('" + ddlIdType.ClientID + "','" + rfvIDType.ClientID + "');");
            //ddlIdType.Attributes.Add("onchange", strClientScript);
        }
    }

    public void ChangeValidateEffect(bool bChange)
    {
        if (bChange)
        {
            ddlIdType.ControlStyle.CssClass = "validationError";
        }
        else
        {
            ddlIdType.ControlStyle.CssClass = "";
        }
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvIDType.Enabled && rfvIDType.ValidationGroup == strValidationGroup)
        {
            rfvIDType.Validate();
            if (rfvIDType.IsValid == false)
            {
                ddlIdType.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlIdType.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlIdType.ControlStyle.CssClass = "";
        }
    }
    //protected void ServerValidation(object source, ServerValidateEventArgs args)
    //{
    //    try
    //    {
    //        // Test whether the value is -1
    //        if (args.Value != "-1")
    //        {
    //            args.IsValid = true;
    //        }
    //        else
    //        {
    //            args.IsValid = false;
    //        }
    //    }

    //    catch (Exception ex)
    //    {
    //        args.IsValid = false;
    //    }
    //}

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            SetReadOnly();
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        rfvIDType.Enabled = bValue;
    }

    public string IdType
    {
        get { return strIdType; }
        set { strIdType = value; }
    }

    public string ValidationGroup
    {
        get { return strValidatinoGroup; }
        set { strValidatinoGroup = value; }
    }

    public string ErrorMessage
    {
        get { return strErrorMessage; }
        set { strErrorMessage = value; }
    }

    public string ClientValidationScript
    {
        get { return strClientScript; }
        set { strClientScript = value; }
    }

    private void SetEnabled()
    {
        //ddlIdType.SelectedIndex = 0;
        ddlIdType.Enabled = bEnabled;
        rfvIDType.Enabled = bEnabled;
    }

    private void SetReadOnly()
    {
        if (bReadOnly)
        {
            ddlIdType.Text = strIdType;

            string orgIdTypeText = ddlIdType.SelectedItem.Text;
            string orgIdTypeValue = ddlIdType.SelectedItem.Value;

            ddlIdType.Items.Clear();
            ddlIdType.Items.Add(new ListItem(orgIdTypeText, orgIdTypeValue));
        }
    }

    protected override void OnPreRender(EventArgs e)
    {

        //SetEnabled();

        if (strIdType != string.Empty)
        {
            if (strIdType != "Select")
                ddlIdType.Text = strIdType;
        }

        if (bReadOnly)
        {
            SetReadOnly();
        }

        if (strValidatinoGroup != null)
        {
            if (strValidatinoGroup != string.Empty)
                rfvIDType.ValidationGroup = strValidatinoGroup;
        }

        if (strErrorMessage != string.Empty)
        {
            //rfvIDType.ErrorMessage = strErrorMessage;
        }

    }

    public string GetIDType()
    {
        strIdType = ddlIdType.SelectedValue;
        return strIdType;
    }

    public bool IsValidRequirement()
    {
        rfvIDType.Validate();
        return rfvIDType.IsValid;
    }
}
